package com.qianfeng.myymatou.view;

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.myymatou.R;
import com.qianfeng.myymatou.bean.ForeignGoodsBean;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/15.
 * <p/>
 * 组合控件
 * “特卖”界面-->“洋货团”列表中单项
 * 包含主题大图和单个Product的展示小图、名称、当前价格、市场价格
 */
public class ForeignGoodsView extends LinearLayout {

    private Context mContext;
    private List<ForeignGoodsBean.DataBean.PagePartsBean.ProductsBean> mProductsList;//横向滚动条中数据源

    private LinearLayout mLlProductsContainer;
    private ImageView mIvGoodsTheme;
    private ImageView mIvSingleProductImg;
    private TextView mTvSingleProductCurrentPrice;
    private TextView mTvSingleProductName;
    private TextView mTvSingleProductMarketPrice;

    public ForeignGoodsView(Context context) {
        this(context, null);
    }

    public ForeignGoodsView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ForeignGoodsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //初始化控件
        initView(context);
    }

    /**
     * 初始化控件
     *
     * @param context 上下文
     */
    private void initView(Context context) {
        mContext = context;
        mProductsList = new ArrayList<>();

        //从布局生成View对象
        LayoutInflater.from(context).inflate(R.layout.view_foreign_goods, this);
        //存放Products的线性容器
        mLlProductsContainer = (LinearLayout) findViewById(R.id.ll_products_container);
        mIvGoodsTheme = (ImageView) findViewById(R.id.iv_goods_theme);

        //对主题图片做监听
        mIvGoodsTheme.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "我是大图！", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 设置数据，由外部调用，传入数据源
     *
     * @param bannerUrl 每项上面大图的url
     * @param list  Products即横向滚动条中展示项数据源集合
     * @param total 当前项的Products总数，对应ForeignGoodsBean中ProductsBean下的Total，用于显示在横向滚动条中的最后一项中
     */
    public void setProductsList(String bannerUrl,List<ForeignGoodsBean.DataBean.PagePartsBean.ProductsBean> list, int total) {
        if (list != null && list.size()>0) {
            mProductsList.addAll(list);
            for (int i = 0; i < mProductsList.size(); i++) {
                //填充单个Product布局，找控件
                LinearLayout llSingleProductContainer = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.item_single_product, null);
                LinearLayout.LayoutParams lpLl=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                lpLl.setMargins(8,0,8,0);//在布局文件中设置margin值并没有什么卵用，所以在这里设置
                llSingleProductContainer.setLayoutParams(lpLl);

                //找出单个Product的各控件
                findViewInSingle(llSingleProductContainer);

                //设置数据到单个Product视图
                pushDataToView(bannerUrl, i);

                //添加单个Product视图到横向滚动条中的横向LinearLayout中
                mLlProductsContainer.addView(llSingleProductContainer);

                //设置单个Product的监听，
                final int finalI = i;
                llSingleProductContainer.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "我是 "+mProductsList.get(finalI).getName(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
            //横向LinearLayout中最后一项，显示“查看全部xx件好货”
            showSeeTotal(total);
        }
    }

    /**
     * 找出单个Product的各控件
     * @param llSingleProductContainer 承装单个Product的各控件的LinearLayout
     */
    private void findViewInSingle(LinearLayout llSingleProductContainer) {
        mIvSingleProductImg = (ImageView) llSingleProductContainer.findViewById(R.id.iv_singleProductImg);
        mTvSingleProductCurrentPrice = (TextView) llSingleProductContainer.findViewById(R.id.tv_singleProductCurrentPrice);
        mTvSingleProductName = (TextView) llSingleProductContainer.findViewById(R.id.tv_singleProductName);
        mTvSingleProductMarketPrice = (TextView) llSingleProductContainer.findViewById(R.id.tv_singleProductMarketPrice);
    }

    /**
     * 将数据设置进对应的单个Product中的控件
     * @param bannerUrl 广告条url
     * @param position 当前位置
     */
    private void pushDataToView(String bannerUrl, int position) {
        Picasso.with(mContext).load(bannerUrl)
                .noFade().placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher).into(mIvGoodsTheme);
        setImageByPicasso(mProductsList.get(position).getPicUrl(), mIvSingleProductImg);
        mTvSingleProductCurrentPrice.setText(mContext.getString(R.string.yuan)+mProductsList.get(position).getPrice());
        mTvSingleProductName.setText(mProductsList.get(position).getName());
        mTvSingleProductMarketPrice.setText(mContext.getString(R.string.yuan)+mProductsList.get(position).getMarketPrice() + "");

        //添加删除线
        mTvSingleProductMarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
    }

    /**
     * 横向LinearLayout中最后一项，显示“查看全部xx件好货”
     * @param total 货物总数
     */
    private void showSeeTotal(final int total) {
        FrameLayout flSeeTotal= (FrameLayout) LayoutInflater.from(mContext).inflate(R.layout.item_see_total,null);
        TextView tvTotal= (TextView) flSeeTotal.findViewById(R.id.tv_total);
        tvTotal.setText(total+"件好货");
        //TODO 缺跳转
        tvTotal.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "有 "+total+" 件好货！", Toast.LENGTH_SHORT).show();
            }
        });
        mLlProductsContainer.addView(flSeeTotal);
    }

    /**
     * 用Picasso设置图片到ImageView
     *
     * @param url 图片url
     * @param ivProducts 要显示到的ImageView
     */
    private void setImageByPicasso(String url, ImageView ivProducts) {
        Picasso.with(mContext)
                .load(url)
                .noFade()
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .resize(80, 80)
                .centerCrop()
                .into(ivProducts);
    }

}

